import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
gtd = pd.read_excel(r'/Users/siddhesh/Library/CloudStorage/OneDrive-UniversityofPittsburgh/Classes/InfoViz/globalterrorismdb_0522dist.xlsx')
gtd.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 209706 entries, 0 to 209705 Columns: 135 entries, eventid to related dtypes: datetime64[ns](1), float64(54), int64(23), object(57) memory usage: 216.0+ MB
gtd.head()
| eventid | iyear | imonth | iday | approxdate | extended | resolution | country | country_txt | region | ... | addnotes | scite1 | scite2 | scite3 | dbsource | INT_LOG | INT_IDEO | INT_MISC | INT_ANY | related | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 197000000001 | 1970 | 7 | 2 | NaN | 0 | NaT | 58 | Dominican Republic | 2 | ... | NaN | NaN | NaN | NaN | PGIS | 0 | 0 | 0 | 0 | NaN |
| 1 | 197000000002 | 1970 | 0 | 0 | NaN | 0 | NaT | 130 | Mexico | 1 | ... | NaN | NaN | NaN | NaN | PGIS | 0 | 1 | 1 | 1 | NaN |
| 2 | 197001000001 | 1970 | 1 | 0 | NaN | 0 | NaT | 160 | Philippines | 5 | ... | NaN | NaN | NaN | NaN | PGIS | -9 | -9 | 1 | 1 | NaN |
| 3 | 197001000002 | 1970 | 1 | 0 | NaN | 0 | NaT | 78 | Greece | 8 | ... | NaN | NaN | NaN | NaN | PGIS | -9 | -9 | 1 | 1 | NaN |
| 4 | 197001000003 | 1970 | 1 | 0 | NaN | 0 | NaT | 101 | Japan | 4 | ... | NaN | NaN | NaN | NaN | PGIS | -9 | -9 | 1 | 1 | NaN |
5 rows × 135 columns
gtd_2020 = gtd[gtd['iyear'] == 2020]
gtd_2020.head()
| eventid | iyear | imonth | iday | approxdate | extended | resolution | country | country_txt | region | ... | addnotes | scite1 | scite2 | scite3 | dbsource | INT_LOG | INT_IDEO | INT_MISC | INT_ANY | related | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 201268 | 202001010001 | 2020 | 1 | 1 | NaN | 0 | NaT | 141 | Nepal | 6 | ... | NaN | "Unidentified groups torch Ncell towers," EKan... | "Unidentified group sets fire on Ncell tower i... | NaN | START Primary Collection | 0 | 0 | 0 | 0 | NaN |
| 201269 | 202001010002 | 2020 | 1 | 1 | NaN | 0 | NaT | 141 | Nepal | 6 | ... | NaN | "Chand-led party cadres, police exchange fire ... | "Improvised explosive device foundat Ncell tow... | NaN | START Primary Collection | 0 | 0 | 0 | 0 | NaN |
| 201270 | 202001010003 | 2020 | 1 | 1 | NaN | 0 | NaT | 200 | Syria | 10 | ... | NaN | "Shelling hit school killing 6 in rebel-held S... | NaN | NaN | START Primary Collection | -9 | -9 | 0 | -9 | NaN |
| 201271 | 202001010005 | 2020 | 1 | 1 | NaN | 0 | NaT | 4 | Afghanistan | 6 | ... | NaN | "1st LD Writethru- Roadside bomb kills 3 civil... | NaN | NaN | START Primary Collection | 0 | 0 | 0 | 0 | NaN |
| 201272 | 202001010006 | 2020 | 1 | 1 | NaN | 0 | NaT | 19 | Bangladesh | 6 | ... | NaN | "Two crude bombs explode in Ramna," Dhaka Trib... | "Two crude bombs explode near Chhatra Dal’s ev... | NaN | START Primary Collection | -9 | -9 | 0 | -9 | NaN |
5 rows × 135 columns
fig, ax = plt.subplots(figsize=(7,12))
sns.set(style='darkgrid')
graph = sns.countplot(data = gtd_2020, y='region_txt', order = gtd_2020['region_txt'].value_counts().index,
hue = 'attacktype1_txt')
for container in graph.containers:
graph.bar_label(container, fontsize=7)
#graph.bar_label(graph.containers[0])
plt.title('Terrorist Attacks in 2020', fontsize = 20, fontweight='bold')
plt.xlabel('Count',fontsize = 15, fontweight='bold')
plt.ylabel('Region', fontsize = 15, fontweight='bold')
plt.legend(title='Attack Type', loc='lower right')
plt.show()
gtd_sa = gtd[gtd['region_txt'] == 'South Asia']
gtd_sa
| eventid | iyear | imonth | iday | approxdate | extended | resolution | country | country_txt | region | ... | addnotes | scite1 | scite2 | scite3 | dbsource | INT_LOG | INT_IDEO | INT_MISC | INT_ANY | related | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 585 | 197011010001 | 1970 | 11 | 1 | NaN | 0 | NaT | 153 | Pakistan | 6 | ... | NaN | NaN | NaN | NaN | PGIS | -9 | -9 | 1 | 1 | NaN |
| 1186 | 197202220004 | 1972 | 2 | 22 | NaN | 0 | NaT | 92 | India | 6 | ... | NaN | NaN | NaN | NaN | Hijacking DB | 1 | 1 | 1 | 1 | NaN |
| 1862 | 197305010002 | 1973 | 5 | 1 | NaN | 0 | NaT | 4 | Afghanistan | 6 | ... | NaN | NaN | NaN | NaN | PGIS | 0 | 1 | 1 | 1 | NaN |
| 2216 | 197402020001 | 1974 | 2 | 2 | NaN | 1 | 1974-02-03 | 153 | Pakistan | 6 | ... | NaN | NaN | NaN | NaN | PGIS | -9 | -9 | 1 | 1 | NaN |
| 2704 | 197412090001 | 1974 | 12 | 9 | NaN | 0 | NaT | 153 | Pakistan | 6 | ... | NaN | NaN | NaN | NaN | PGIS | -9 | -9 | 1 | 1 | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 209691 | 202012310004 | 2020 | 12 | 31 | NaN | 0 | NaT | 4 | Afghanistan | 6 | ... | Casualties conflict across sources. Following ... | "Afghan War Casualty Report: December 2020," N... | "Afghan media highlights 31 Dec 20," BBC Monit... | "Terrorism Digest: 30-31 Dec 20," BBC Monitori... | START Primary Collection | -9 | -9 | 0 | -9 | NaN |
| 209692 | 202012310005 | 2020 | 12 | 31 | NaN | 0 | NaT | 92 | India | 6 | ... | NaN | "Police access CCTV footage, get leads in jewe... | "Goldsmith shot dead, CRPF officer injured in ... | "Jeweller shot dead in Srinagar," Daily Excels... | START Primary Collection | 0 | 0 | 0 | 0 | NaN |
| 209693 | 202012310006 | 2020 | 12 | 31 | NaN | 0 | NaT | 92 | India | 6 | ... | The victims included Hardayal Yadav. | "Jeweller shot dead in Srinagar," Daily Excels... | "Goldsmith shot dead by militants in Srinagar,... | "CRPF officer injured in firing by militants i... | START Primary Collection | -9 | -9 | 0 | -9 | NaN |
| 209700 | 202012310014 | 2020 | 12 | 31 | NaN | 0 | NaT | 141 | Nepal | 6 | ... | NaN | "Govt vehicle torched in Jajarkot," Khabar Hub... | NaN | NaN | START Primary Collection | -9 | -9 | 0 | -9 | NaN |
| 209704 | 202012310018 | 2020 | 12 | 31 | NaN | 0 | NaT | 4 | Afghanistan | 6 | ... | NaN | "Civil society activist and tribal elder kille... | "Terrorism Digest: 1-2 Jan 21," BBC Monitoring... | NaN | START Primary Collection | -9 | -9 | 0 | -9 | NaN |
54725 rows × 135 columns
gtd_sa.rename(columns={'iyear':'Year','imonth':'Month','iday':'Day','country_txt':'Country',
'region_txt':'World_Region','attacktype1_txt':'Attack_Type','target1':'Target',
'nkill':'Killed','nwound':'Wounded','summary':'Summary','gname':'Militant_Group',
'targtype1_txt':'Target_Type','weaptype1_txt':'Weapon_Type','motive':'Motive'},inplace=True)
gtd_sa =gtd_sa[['Year','Month','Day','Country','World_Region','city','latitude','longitude','Attack_Type','Killed',
'Wounded','Target','extended','Militant_Group','Target_Type','Weapon_Type','Motive']]
/var/folders/qr/2xmc9mzj767bg9z6g_q4k9180000gn/T/ipykernel_82517/2436188294.py:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
gtd_sa.rename(columns={'iyear':'Year','imonth':'Month','iday':'Day','country_txt':'Country',
gtd_sa.head()
| Year | Month | Day | Country | World_Region | city | latitude | longitude | Attack_Type | Killed | Wounded | Target | extended | Militant_Group | Target_Type | Weapon_Type | Motive | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 585 | 1970 | 11 | 1 | Pakistan | South Asia | Karachi | 24.891115 | 67.143311 | Assassination | 4.0 | 20.0 | Deputy Foreign Minister: Zygfryd Wolniak | 0 | Unknown | Government (Diplomatic) | Vehicle (not to include vehicle-borne explosiv... | NaN |
| 1186 | 1972 | 2 | 22 | India | South Asia | New Delhi | 28.585836 | 77.153336 | Hijacking | 0.0 | 0.0 | B-747 | 0 | Palestinians | Airports & Aircraft | Explosives | NaN |
| 1862 | 1973 | 5 | 1 | Afghanistan | South Asia | Kabul | 34.516895 | 69.147011 | Unknown | 0.0 | 1.0 | unk, asst. mgr., Indian Airlines, Kabul | 0 | Black December | Airports & Aircraft | Unknown | NaN |
| 2216 | 1974 | 2 | 2 | Pakistan | South Asia | Karachi | 24.891115 | 67.143311 | Bombing/Explosion | 0.0 | 0.0 | Greek Freighter in Karachi | 1 | Muslim Guerrillas | Maritime | Firearms | NaN |
| 2704 | 1974 | 12 | 9 | Pakistan | South Asia | Peshawar | 34.006004 | 71.537430 | Bombing/Explosion | 0.0 | 2.0 | USIS Center | 0 | Unknown | Government (Diplomatic) | Explosives | NaN |
gtd_sa_groups=gtd_sa[gtd_sa['Militant_Group'].isin(gtd_sa['Militant_Group'].value_counts()[1:11].index)]
pd.crosstab(gtd_sa_groups.Year, gtd_sa_groups.Militant_Group).plot(color=sns.color_palette('Paired',10))
sns.set(style='darkgrid')
fig=plt.gcf()
fig.set_size_inches(18,6)
plt.title('South Asia - Top Terror Operators Over the Years', fontsize = 15, fontweight='bold')
plt.xlabel('Year',fontsize = 12, fontweight='bold')
plt.ylabel('Count', fontsize = 12, fontweight='bold')
plt.show()
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
pio.renderers.default = 'vscode+notebook'
gtd_sa_map = gtd_sa['Militant_Group'].value_counts()[1:11].index
filtered_data = gtd_sa[gtd_sa['Militant_Group'].isin(gtd_sa_map)]
fig = px.scatter_mapbox(filtered_data, lat='latitude', lon='longitude',
hover_name='Militant_Group', color='Militant_Group',
title='Attacks of Militant Groups across South Asia',
zoom=1, height=800)
fig.update_layout(mapbox_style="carto-positron")
fig.update_geos(projection_type="orthographic")
fig.update_traces(marker=dict(size=10, opacity=0.5))
fig.show(renderer="notebook")